// 全局变量

var MY_SM_PRESCRIPTION_VIOLATE = (function() {

	/**
	 * 查看明细
	 * */
	function getParams(){
		
		var params = {};
		
		// params.dateYm = $('#year').val() + '' + $('#month').val();
		params.dtFrom = $('#dtFrom').val();
		params.dtTo = $('#dtTo').val();

		params.institutionId = $('#institutionId').val();
		// 类别
		if ($('#msgId').val()!=9){
			params.msgId = $('#msgId').val();
		}
		// 处理状态
		if ($('#followStatus').val()!=9){
			params.followStatus = $('#followStatus').val();
		}
		
		return params;
	}
	return {
		getParams:getParams
	}
})();

$(document).ready(function() {
	
	// 下拉框变化时刷新 
	$('#msgId,#checkSt').on('change',function(){
		
		var $thead = $('#thead-prescription');
		var $tbody= $('#tbody-prescription');
		var params = MY_SM_PRESCRIPTION_VIOLATE.getParams();
		
		MY_PRESCRIPTION_VIOLATE_COM.loadDetailItem(params,$thead,$tbody)
	});
	
	// 显示申诉图片
	$('#tbody-prescription').on('click','button[name="showAppealAtta"]',function(){
		var $that = $(this);
		var $parentTr = $that.closest('tr');
		
		var appealAtta = $parentTr.data('appeal_atta');
		var institutionId = $('#institutionId').val();
		// 显示交接单图片
		MY_SHOW_IMG.createShowImgModal(institutionId ,appealAtta);
	});
	
	// 处理申诉 OR 取消异常   注：申诉处理由于多个页面共用，故提炼成appeal.js
	$('#tbody-prescription').on('click','button[name="closeIllegal"],button[name="check"]',function(){
		var $that = $(this);
		var $parentTr = $that.closest('tr');
		
		var illegalId = $parentTr.data('illegal_id');
		var originBtnName= $that.prop('name');
		// 更新用模态框
		var params = MY_SM_PRESCRIPTION_VIOLATE.getParams();
		// 页面初期数据查询
	    var $thead= $('thead#thead-prescription');
	    var $tbody= $('tbody#tbody-prescription');// 明细
	    
		MY_APPEAL.createModalWin
		(illegalId,
				originBtnName,
			function(){ MY_PRESCRIPTION_VIOLATE_COM.loadDetailItem(params,$thead,$tbody);},
			function(){ MY_SUMMARIZE.getSummarize();}
		);
	});
	
//	// 跟进
//	var $tbodyPrescriptionDetailList = $('#tbodyPrescriptionDetailList');
//	$tbodyPrescriptionDetailList.on('click','button[name="follow_up"]',function(){
//		
//		var $that = $(this);
//		var $parentTr = $that.closest('tr');
//		
//		WeKnow.loading();
//		
//		$.ajax({ 
//	        type: "POST", 
//	        url: "invs_prescription_violate.do/update", 
//	        data: {violateId:$parentTr.data('violate_id'),institutionId:$parentTr.data('institution_id')},
//	        async: true,//异步调用
//	        success: function(data) {
//	        	
//	        	WeKnow.closeLoading();
//	        	
//				if (!WeKnow.filterAjaxResult(data)) return false; // session过期check
//	        	
//	        	if (data.errcode == 0) {
//	        		$parentTr.find('.follow-status').text('已跟进');
//	        		$parentTr.find('button[name="follow_up"]').remove();//删除跟进按钮
//	        		WeKnow.successMsg('更新成功');
//	        	} else if (data.errcode == -1 ) {
//	        		WeKnow.errorMsg( data.errMsg );
//	        	} else {
//	        		WeKnow.systemMsg('更新失败，请重新登录后再试');
//	        	}
//	        }
//		});
//	});
	
	// 查看处方
	$('#tbody-prescription').on('click','button[name="showPre"]',function(){
		
		var $that = $(this);
		var $parentTr = $that.closest('tr');
		
		WeKnow.loading();
		
		$.ajax({ 
	        type: "POST", 
	        url: "invs_prescription_violate.do/show_prescription", 
	        data: {violateId:$parentTr.data('violate_id'),institutionId:$parentTr.data('institution_id')},
	        async: true,//异步调用
	        success: function(data) {
	        	
	        	WeKnow.closeLoading();
	        	
				if (!WeKnow.filterAjaxResult(data)) return false; // session过期check
	        	
	        	if (data.errcode == 0) {
	        		MY_WEST_PRESCRIPTION.resetPrintBody(data.outpatient);
	        		var keepAttr = ["class", "style"];
	    			var options = {retainAttr: keepAttr};
	    			$('#printDiv').printArea(options);
	        	} else if (data.errcode == -1 ) {
	        		WeKnow.errorMsg( data.errMsg );
	        	} else {
	        		WeKnow.systemMsg('查看处方失败，请重新登录后再试');
	        	}
	        }
		});
	});
});